/**
 * Created by yqj on 2017/2/20.
 */
const mongoose = require('mongoose'),
    Schema = mongoose.Schema;

const roleSchema = new Schema({
    id: {type: Schema.Types.ObjectId},
    key: {type: String},
    name: {type: String},
    updated_at: {type: Date, default: Date.now()},
    created_at: {type: Date, default: Date.now()}
});

/**
 * 根据角色和账号前缀查询
 * @param role
 * @param prefix
 * @returns {T|*}
 */
roleSchema.query.byRoleAndAccountPrefix = (role, prefix) => {
    console.log(role, prefix)
    if (prefix) {
        return this.find({
            role: role,
            loginAccount: new RegExp(prefix)
        });
    } else {
        return this.find({
            role: role
        });
    }
};

roleSchema.query.byLimit = (limit) => {
    return this.limit(limit);
};

roleSchema.query.getCount = (callback) => {
    return this.count(callback);
};

module.exports = roleSchema;